var game_config = require("./game_config")
import mouse from "./mouse"
import hammer from "./hammer"
import score_show from "./score_show"
import score_value from "./score_value"
    export default class game_mgr extends Laya.Script {
 /** @prop {name:mouse_prefab, tips:"老鼠预制", type:Prefab, default:null}*/
 /** @prop {name:mouse_root, tips:"老鼠父节点", type:Node, default:null}*/
 /** @prop {name:hammer, tips:"锤子对象", type:Node, default:null}*/
 /** @prop {name:score_prefab, tips:"分数预制", type:Prefab, default:null}*/
 /** @prop {name:score_root, tips:"分数节点", type:node, default:null}*/
 /** @prop {name:score_value_label, tips:"分数", type:node, default:null}*/
 /** @prop {name:speed, tips:"速度", type:Int, default:1500}*/
  // 
    constructor() { 
        super(); 
    //    this.is_start = false;
        this.mouse_prefab = null;
        this.mouse_root = null;
        this.hammer = null;
       this.score_prefab = null;
       this.score_root = null;
       this.score_value_label = null;
       this.speed = 1500;
       
      // this.score = 0;
    }
    
    onEnable() {
    }

    onDisable() {
    }

    onStart(){
        this.hammer_component = this.hammer.getComponent(hammer);
        this.game_start();
        this.score_value = this.score_value_label.getComponent(score_value);
        this.score_value.init(this);
     //   console.log(this.score_value.score)
     //   console.log(this.score_value_label.getComponent(Laya.Label).text );
     //   console.log("-----");
     //   console.log(this.score_value_label == null);
        
    }

    game_start(){
      //  this.is_start = true;
        this.gen_one_mouse();
    }

    gen_one_mouse(){
        var mouse_type = Math.random()<0.2?1:2;
        var m = this.mouse_prefab.create();
        this.mouse_root.addChild(m);
        var hole_index = Math.random() * 9;
        hole_index = Math.floor(hole_index);


        m.x = game_config.mouse_pos[hole_index].x;
        m.y = game_config.mouse_pos[hole_index].y;
        m.getComponent(mouse).show_mouse(this,mouse_type,hole_index);

        
        
        var time = 2+Math.random();
        Laya.timer.once(time*this.speed,this,this.gen_one_mouse);

    }

    on_mouse_hit(mouse_type,hole_index){
        this.hammer.x =game_config.hammer_pos[hole_index].x;
        this.hammer.y =game_config.hammer_pos[hole_index].y;

        this.hammer_component.play_hammer_anim();

        var s = this.score_prefab.create();
        this.score_root.addChild(s);
        s.x = game_config.score_pos[hole_index].x;
        s.y =game_config.score_pos[hole_index].y;
        
        var flag = (mouse_type==1);

        s.getComponent(score_show).show_score(flag)
        this.score_value.addScore(flag);
    }
}